package com.cgnpc.scp.hse.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.StaffTysInfoWithQuery;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.HrCenterService;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/******************************
 * 用途说明: SCP人员信息
 * 作者姓名: yblrs
 * 创建时间: 2020/8/20 22:20
 ******************************/
@Controller
@RequestMapping("/scp/hse/person")
public class HseScpPersonController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HseScpPersonController.class);

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private HrCenterService hrCenterService;

    @Autowired
    private MessageSource messageSource;

    /**********************************
     * 用途说明: SCP人员信息---模糊查询
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "SCP人员信息---模糊查询")
    @UBA(module = "调试HSE管理", action = "人员查询", channel = OperatorType.Button)
    @RequestMapping(value = "/query", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends StaffTysInfoWithQuery> Object query(HttpServletRequest request, @RequestBody T reqEntity) {
        // final String logInfo = "[HseScpPersonController_query_UUID_" + UUID.randomUUID() + "]";
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
//            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

//            String reqStaffNo = reqEntity.getStaffNo(); // staffNo 员工号
//            String reqStaffName = reqEntity.getStaffName(); // staffName 员工姓名

            /**
             * 当前返回的值：
             * "staffNo": "P622400",
             * "staffName": "[P622400]潘瑞辉",
             * "staffNamePy": "",
             * "staffShortPy": "",
             * "uniocomphone": "18680320361",
             * "officeAddr": "广东省深圳市中广核大厦北楼4A003"
             */

            ApiResult apiResult = hrCenterService.getStaffInfoByKeywordWithPager(reqEntity);
            // List<StaffTysInfo> resultStaffTysInfoList = null;
            // resultStaffTysInfoList的类型为List<Map>
            Object resultStaffTysInfoList = apiResult == null ? null : apiResult.getData();

            ajaxResult = AjaxResultUtil.createI18nSuccessDefaultMessage(reqLocale, messageSource);
            // ajaxResult.put("apiResult", apiResult);
            // ajaxResult.put("apiResult-StringBuilder", apiResult == null ? null : ToStringBuilder.reflectionToString(apiResult));
            ajaxResult.put("userResults", resultStaffTysInfoList);
        } catch (Exception e) {
            LOGGER.error("query.error;", e);
            ajaxResult = AjaxResultUtil.createI18nErrorDefaultMessage(request, messageSource);
        }

        return ajaxResult;
    }

}
